package com.libre.view.file;

import com.libre.constant.SysConstant;
import com.libre.event.DragDroppedEvent;
import com.libre.event.DragOverEvent;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.*;

/**
 * 打印
 */
public class PrintView extends Stage {

    TableView<Map<String, Object>> table = new TableView<>();
    private List<Map<String, Object>> list = new ArrayList<>();

    public PrintView(BorderPane borderPane) {
        // 上传组件
        Button button = new Button();
        button.setMinWidth(SysConstant.DEFAULT_WIDTH - 20);//宽度
        // 方式1：拖拽上传
        button.setOnDragOver(new DragOverEvent(table));
        button.setOnDragDropped(new DragDroppedEvent(table, list));
        // 方式2：选中上传
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("请选择文件");
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File home = fsv.getHomeDirectory();
        String savePath = home.getPath();//桌面绝对路径
        fileChooser.setInitialDirectory(new File(savePath));
        button.setOnAction(e -> {
            List<File> files = fileChooser.showOpenMultipleDialog(this);//选择多个文件
            DragDroppedEvent.add(table, list, files);
        });

        // 上传样式
        Image image = new Image(SysConstant.UPLOAD_ICON);
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(50);
        imageView.setFitHeight(50);
        //给按钮设置图标
        button.setGraphic(imageView);
        button.setText("拖拽文件开始上传或者点击选中上传");

        // 按钮
        HBox hBox = new HBox();
        hBox.setStyle("-fx-alignment:top-right;");
        TextField textField = new TextField();
        Button button1 = new Button("搜索");
        Button button2 = new Button("打印");
        Button button3 = new Button("全选");
        Button button4 = new Button("反全选");
        hBox.getChildren().addAll(textField, button1, button2, button3, button4);

        // Table 数据
        TableColumn filePathCol = new TableColumn("文件");// 文件列
        filePathCol.setMinWidth(SysConstant.DEFAULT_WIDTH - 220); // 设置宽度
        /*firstNameCol1.setCellValueFactory(
                new PropertyValueFactory<>("a"));//实体类*/
        filePathCol.setCellValueFactory(new MapValueFactory<>("filePath"));
        TableColumn operateCol = new TableColumn("操作");// 操作列
        operateCol.setCellValueFactory(new MapValueFactory<>("operate"));
        operateCol.setMinWidth(200); // 设置宽度

        table.getColumns().addAll(filePathCol, operateCol); //一次添加列进TableView

        VBox vBox = new VBox();
        vBox.getChildren().addAll(button, hBox, table);
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(vBox);
        scrollPane.setPadding(new Insets(10));
        borderPane.setCenter(scrollPane);
    }

}
